package models

import (
	"fmt"
	"github.com/gin-gonic/gin"
)

type student struct {
	Sno          string     `gorm:"column:sno"`
	Sname        string     `gorm:"column:sname"`
	Sclass       string     `gorm:"column:sclass"`
	Smajor       string     `gorm:"column:smajor"`
	Sdept        string     `gorm:"column:sdept"`
	Mbti_outcome string     `gorm:"column:mbti_outcome"`
	TestAnswer TestAnswer `gorm:"embedded"`
	FormatTime string
}

func GetAllStudent(studentParam map[string]interface{}) (error, []student, int64) {
	var studentdata []student
	page := studentParam["page"].(int)
	pageSize := studentParam["limit"].(int)
	searchText := ""
	fmt.Println(studentParam["Sname"])
	if(studentParam["Sname"] != ""){
		searchText += " and \"Student\".\"sname\" like '%"+ studentParam["Sname"].(string)+"%'"
	}
	var total int64

	err := db.Select("\"Student\".\"sno\",\"Student\".\"sname\",\"Student\".\"sclass\",\"Student\".\"smajor\",\"Student\".\"sdept\",\"Student\".\"mbti_outcome\",\"Test_answer_sheet\".\"tas_id\",\"Test_answer_sheet\".\"test_time\",\"Test_answer_sheet\".\"result\"").Table("Student").Joins("right join \"Test_answer_sheet\" on \"Test_answer_sheet\".sno = \"Student\".sno").Where("\"Test_answer_sheet\".\"result\"!=''"+searchText).Order("sno ASC").Count(&total).Offset((page - 1) * pageSize).Limit(pageSize).Find(&studentdata).Error

	for i := 0; i < len(studentdata); i++ {
		studentdata[i].FormatTime = studentdata[i].TestAnswer.Test_time.Format("2006-01-02 15:04:05")
	}

	return err, studentdata, total
}

func AddStudent(a *gin.Context) (error, student) {
	var studentdata student
	a.BindJSON(&studentdata)
	fmt.Printf("%+v \n", studentdata)
	err := db.Table("Student").Select("sno", "sname", "sclass", "smajor", "sdept", "mbti_outcome").Create(&studentdata).Error
	return err, studentdata
}

func AddStudent2(studentdata student) (error, student) {
	fmt.Printf("%+v \n", studentdata)
	err := db.Table("Student").Select("sno", "sname", "sclass", "smajor", "sdept", "mbti_outcome").Create(&studentdata).Error
	return err, studentdata
}

func UpdateStudent2(studentdata student) (error, student) {
	err := db.Table("Student").Where("sno = ?", studentdata.Sno).Updates(&studentdata).Error
	return err, studentdata
}

func UploadStudent(c *gin.Context) (error, student) {
	var studentData, Data student
	var total int64
	c.BindJSON(&studentData)
	err := db.Table("Test_answer_sheet").Where("sno = ?", studentData.Sno).Find(&Data).Count(&total).Error
	fmt.Println(total)
	if total > 0 {
		fmt.Println("修改")
		UpdateStudent2(studentData)
	} else {
		fmt.Println("增加")
		AddStudent2(studentData)
	}

	return err, studentData
}

func UpdateStudent(a *gin.Context) (error, student) {
	var studentdata student
	a.BindJSON(&studentdata)
	err := db.Table("Student").Where("sno = ?", studentdata.Sno).Updates(&studentdata).Error
	return err, studentdata
}

func UpdateByParam(stu student) (error, student) {
	err := db.Table("Student").Where("sno = ?", stu.Sno).Updates(&stu).Error
	return err, stu
}

func DelStudent(a *gin.Context) error {
	sno := a.Params.ByName("sno")
	err := db.Table("Student").Where("sno = ?", sno).Delete("sno").Error //有问题，记得改
	return err
}
